<template>
  <div class="container">
    <a-card class="general-card" title="已享受补贴居民查询">
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="formModel"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="left"
          >
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-item field="cardId" label="居民身份证号码">
                  <a-input
                    v-model="formModel.cardId"
                    placeholder="请输入居民身份证号码"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-space :size="18">
                  <a-button type="primary" @click="search">
                    <template #icon>
                      <icon-search />
                    </template>
                    {{ $t('searchTable.form.search') }}
                  </a-button>
                  <a-button @click="reset">
                    <template #icon>
                      <icon-refresh />
                    </template>
                    {{ $t('searchTable.form.reset') }}
                  </a-button>
                </a-space>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData.rows"
        :bordered="false"
        :pagination="{
          total: renderData.total,
          pageSize: formModel.rows,
          showTotal: true,
          showPageSize: true,
        }"
        @page-change="onPageChange"
        @page-size-change="onPageSizeChange"
      >
        <template #columns>
          <a-table-column title="序号" :width="60">
            <template #cell="{ rowIndex }">
              {{ rowIndex + 1 }}
            </template>
          </a-table-column>
          <a-table-column
            title="居民身份证号码"
            data-index="cardId"
            width="240"
          />
          <a-table-column title="姓名" data-index="name" width="200" />
          <a-table-column title="性别" data-index="gender" />
          <a-table-column
            title="首次认定人员类型"
            data-index="personBizTypeName"
            width="200"
          />
          <a-table-column title="理论可享受月数" data-index="canApplyMonths" />
          <a-table-column title="累计已享受月数" data-index="applyedMonths" />
          <a-table-column title="初次享受月份合法" data-index="fstApplyStartMonthLegalFlag">
            <template #cell="{ record }">
              {{ record.fstApplyStartMonthLegalFlag === '1' ? '是' : '否' }}
            </template>
          </a-table-column>
          <a-table-column
            :title="$t('columns.operations')"
            align="right"
            data-index="operations"
            :width="100"
          >
            <template #cell="{ record }">
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record)"
              >
                详情
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import {
    applyPersons,
    PersonInfoRes,
    PersonInfoVO,
    PersonQryDTO,
    TUser,
  } from '@/api/subsidy';

  const generateFormModel = () => {
    return {
      cardId: '',
      page: 1,
      rows: 20,
    };
  };
  const emit = defineEmits(['go']);
  const { loading, setLoading } = useLoading();
  const renderData = ref<PersonInfoRes>({
    rows: [],
    total: 0,
  }); // ref<PersonVo[]>([]);
  const formModel = ref(generateFormModel());

  const fetchData = async (
    params: PersonQryDTO // = { current: 1, pageSize: 20 }
  ) => {
    setLoading(true);
    try {
      const { data } = await applyPersons(params);
      renderData.value = data;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const handleClickToSave = (obj?: PersonInfoVO) => {
    emit('go', '2', obj);
  };
  const search = () => {
    fetchData(formModel.value as PersonQryDTO);
  };

  const onPageChange = (current: number) => {
    formModel.value.page = current;
    search();
  };
  const onPageSizeChange = (current: number) => {
    formModel.value.rows = current;
    search();
  };
  search();

  const reset = () => {
    formModel.value = generateFormModel();
  };
</script>

<style scoped lang="less">
  .container {
    padding: 10px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
